import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ImageUtils {
  /// 构建圆角图片
  /// Container+BoxDecoration
  static Widget buildRoundImg(url, {height = 200.0, radius = 12.0}) {
    return Container(
      width: double.infinity,
      decoration: ShapeDecoration(
        image: DecorationImage(
            fit: BoxFit.cover,
            image: NetworkImage(
              url,
            )),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadiusDirectional.circular(radius)),
      ),
      height: height, // 必须设置。否则不会显示
    );
  }

  /// 构建圆角图片
  /// Container+BoxDecoration
  static Widget buildRoundImg2(url,
      {width = 200, height = 200.0, radius = 12.0}) {
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(radius),
          image: DecorationImage(
              fit: BoxFit.cover, // 这个属性一定要加，否则会看不到圆角
              image: NetworkImage(url))),
    );
  }

  /// 构建圆角图片
  /// Card的shape属性
  static Widget buildRoundImgByCard(url, {ratio: 16 / 9, radius = 12.0}) {
    return Card(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadiusDirectional.circular(radius)),
      clipBehavior: Clip.antiAlias,
      // child: Image.network(entry.imageUrl,fit: BoxFit.cover,),
      child: AspectRatio(
        aspectRatio: ratio,
        child: Image.network(
          url,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  /// 构建圆角图片
  /// ClipRRect
  static Widget buildRoundImgByClip(url, {ratio: 16 / 9, radius: 12.0}) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(radius),
      child: AspectRatio(
        aspectRatio: ratio,
        child: Image.network(
          url,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  /// 构建圆角图片
  static Widget buildRoundImgByClipRRect(aspectRatio, radius, src) {
    return AspectRatio(
      aspectRatio: aspectRatio,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: Image.network(
          src,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}
